
/*
* by huxy
* time: 2021-07-24
* dec： 带高程和地形的曲面
*/

export default class Add3DTileset {
    constructor() {
        this.tileModelTool = {
            scale: 1.0,
            longitude: 0,
            latitude: 0,
            height: 0, //修改高度
            rx: 0,
            ry: 0,
            rz: 0, //修改旋转
            alpha: 1.5
        };
        this.index = 0;
        this.DataSource = null;
        this.tilesetObj = null;
        this.manager = null;
    }
    SourceshowHide() { }
    initManager() {
        this.manager = new Cesium.PrimitiveCollection();
        this.manager.destroyPrimitives = true
    }
    deletes() {
        // if (!this.manager.isDestroyed()) {
        //     this.manager && this.manager.removeAndDestroy()
        // }
        // if (this.DataSource && this.DataSource._primitives && !this.DataSource._primitives.isDestroyed()) {

        //     this.DataSource && this.DataSource._primitives.removeAndDestroy();

        // }
    }
    init(options) {
        debugger
        this.manager.destroyPrimitives = false;
        var tileset = this.tilesetObj = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
            url: options.tileUrl,
            // maximumScreenSpaceError: 16,
            // maximumNumberOfLoadedTiles: 512,
        }));
        this.manager.add(this.tilesetObj);
        viewer.scene.globe.depthTestAgainstTerrain = true;

        viewer.scene.primitives.add(this.manager);
        const that = this;
        tileset.readyPromise
            .then((currentModel) => {
                var scene = viewer.scene;
                var globe = scene.globe;
                window.tileModel = currentModel;
                viewer.zoomTo(
                    currentModel,
                    new Cesium.HeadingPitchRange(-0.5, -5.0, tileset.boundingSphere.radius * 1.0)
                );
                //获取模型中心点经纬度坐标
                that.tileModelTool.longitude = options.center[0]; //  (cartographic.longitude / Math.PI) * 180;
                that.tileModelTool.latitude = options.center[1];//  (cartographic.latitude / Math.PI) * 180;
                that.tileModelTool.height = options.center[2]; // cartographic.height / 2;
                //修改3dtiles位置，input，range组件的change事件绑定此函数
                that.update3dtilesMaxtrix();
                //模型点击事件
                // attachTileset(viewer, currentModel);
                // that.tileModelToolVisiable = true; //显示3dtiles调整工具
            })
            .otherwise(function (error) {
                new Error(error);
            });
        return tileset
    }
    update3dtilesMaxtrix() {
        var mx = Cesium.Matrix3.fromRotationX(
            Cesium.Math.toRadians(this.tileModelTool.rx || 0)
        );
        var my = Cesium.Matrix3.fromRotationY(
            Cesium.Math.toRadians(this.tileModelTool.ry || 0)
        );
        var mz = Cesium.Matrix3.fromRotationZ(
            Cesium.Math.toRadians(this.tileModelTool.rz || 0)
        );
        var rotationX = Cesium.Matrix4.fromRotationTranslation(mx);
        var rotationY = Cesium.Matrix4.fromRotationTranslation(my);
        var rotationZ = Cesium.Matrix4.fromRotationTranslation(mz);
        //平移 修改经纬度
        var position = Cesium.Cartesian3.fromDegrees(
            this.tileModelTool.longitude,
            this.tileModelTool.latitude,
            this.tileModelTool.height
        );
        var m = Cesium.Transforms.eastNorthUpToFixedFrame(position);
        //旋转、平移矩阵相乘
        Cesium.Matrix4.multiply(m, rotationX, m);
        Cesium.Matrix4.multiply(m, rotationY, m);
        Cesium.Matrix4.multiply(m, rotationZ, m);
        //缩放 修改缩放比例
        var scale = Cesium.Matrix4.fromUniformScale(this.tileModelTool.scale);
        Cesium.Matrix4.multiply(m, scale, m);
        //赋值给tileset
        window.tileModel._root.transform = m;
        //调整地下透明度
        viewer.scene.globe.translucency.frontFaceAlphaByDistance.nearValue = Cesium.Math.clamp(
            this.tileModelTool.alpha,
            0.0,
            1.0
        );
    }
    SourceshowHide() { }
    drawTheBillboard(lon, lat, h) {
        this.DataSource.entities.add({
            position: Cesium.Cartesian3.fromDegrees(lon, lat, h),
            billboard: {
                image: require("../../assets/materialImg/point1.png"),  // "../../../asset/materialImg/line-markder.png",
                width: 48,
                height: 48,
                scale: 0.5,
                sizeInMaters: true,
                // clampToGround: true, //折线固定在地面
            }
        })
        viewer.flyTo(this.DataSource);
    }
    drawOutLIne(data) {

        viewer.entities.add({
            id: 'modelOutLIne',
            polyline: {
                positions: Cesium.Cartesian3.fromDegreesArray(data.coordinates),
                width: 1,
                material: Cesium.Color.TURQUOISE.withAlpha(0.5),//  Cesium.Color.ORANGE.withAlpha(0.6),
                // depthFailMaterial: Cesium.Color.BLUE, //位于地下的材质,不给地下的材质，位于地下的线就不可见
                clampToGround: false, //折线固定在地面
                arcType: Cesium.ArcType.GEODESIC, //定义连接点采用的路径
            },
        })
        // viewer.dataSources.add(this.DataSource);
    }
}